using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Konst3d.Efficiency.Suite;
using Konst3d.Efficiency.Suite.SparsedBinary;

namespace DevUI
{
    public partial class MonitorStatisticsForm : Form
    {
        private static int[] DAY_OFFSET = { 6, 0, 1, 2, 3, 4, 5 };
        private IMonitor _monitor;

        private class StatisticListEntry
        {
            public IStatisticDay Day;

            public override string ToString()
            {
                return "[" + Day.Date + "] " + Day.Value + " [" + Day.Active + "]";
            }
        }

        public MonitorStatisticsForm()
        {
            InitializeComponent();
        }

        public void ShowStatistic(IMonitor monitor)
        {
            _monitor = monitor;

            uiMonitorNameLabel.Text = monitor.Title;
            uiMonitorTypeTextBox.Text = monitor.Type.ToString();

            Show();

            int index = uiStatisticRangeComboBox.FindString("week");
            uiStatisticRangeComboBox.SelectedIndex = index;

            DateTime first = GetFirstDayOfWeek();

            ShowRange(first, first.AddDays(6));
            
        }

        private void ShowRange(DateTime start, DateTime end)
        {
            var days = _monitor.Statistic.GetRange(start, end);

            uiMonitorStatisticListBox.Items.Clear();

            foreach (var day in days)
            {
                uiMonitorStatisticListBox.Items.Add(new StatisticListEntry(){ Day = day });
            }

            switch (_monitor.Type)
            {
                case MonitorType.Monitor:
                    uiMonitorResultNameLabel.Text = "Period Efficiency: ";
                    uiMonitoringResultLabel.Text = _monitor.GetAverageValue(start, end) + "%";
                    break;

                case MonitorType.Counter:
                    uiMonitorResultNameLabel.Text = "Count: ";
                    uiMonitoringResultLabel.Text = _monitor.GetSummaryValue(start, end).ToString();
                    break;

                case MonitorType.Commandment:
                    uiMonitorResultNameLabel.Text = "Commandment violations:";
                    uiMonitoringResultLabel.Text = _monitor.GetCommandmentViolationsCount(start, end).ToString();
                    break;
            }
        }

        private DateTime GetFirstDayOfWeek()
        {
            /*
            public enum DayOfWeek
            {
                Sunday = 0,
                Monday = 1,
                Tuesday = 2,
                Wednesday = 3,
                Thursday = 4,
                Friday = 5,
                Saturday = 6,
            }
            */
            return DateTime.Now.AddDays(-(double)DAY_OFFSET[(int)DateTime.Now.DayOfWeek]);
        }

        private int GetNumDaysInMonth(int month) // 1 - 12
        {
            int result = 0;

            switch (month)
            {
                case 1: result = 31; break;
                case 2: result = DateTime.IsLeapYear(DateTime.Now.Year) ? 29 : 28; break;
                case 3: result = 31; break;
                case 4: result = 30; break;
                case 5: result = 31; break;
                case 6: result = 30; break;
                case 7: result = 31; break;
                case 8: result = 31; break;
                case 9: result = 30; break;
                case 10: result = 31; break;
                case 11: result = 30; break;
                case 12: result = 31; break;
            }

            return result;
        }

        private void uiStatisticRangeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (uiStatisticRangeComboBox.SelectedIndex)
            {
                case 0: // week
                    ShowRange(GetFirstDayOfWeek(), GetFirstDayOfWeek().AddDays(6));
                    break;
                case 1: // month
                    ShowRange(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), new DateTime(DateTime.Now.Year, DateTime.Now.Month, GetNumDaysInMonth(DateTime.Now.Month)));
                    break;
                case 2: // year
                    ShowRange(new DateTime(DateTime.Now.Year, 1, 1), new DateTime(DateTime.Now.Year, 12, 31));
                    break;
                case 3: // all
                    ShowRange(new DateTime(_monitor.Created.Year, 1, 1), new DateTime(_monitor.Created.Year + 99, 12, 31));
                    break;
                case 4:
                    MessageBox.Show("custom"); break;
            }
        }

        private void uiSaveMonitorDayChangesButton_Click(object sender, EventArgs e)
        {
            if (uiMonitorStatisticListBox.SelectedIndex >= 0)
            {
                var entry = uiMonitorStatisticListBox.SelectedItem as StatisticListEntry;

                entry.Day.Active = uiMonitorDayActive.Checked;
                entry.Day.Commentary = uiMonitorDayCommentary.Text;
                entry.Day.Value = (float)uiDayValueUpDown.Value;
            }
        }

        private void uiMonitorStatisticListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uiMonitorStatisticListBox.SelectedIndex >= 0)
            {
                var entry = uiMonitorStatisticListBox.SelectedItem as StatisticListEntry;

                uiMonitorDayActive.Checked = entry.Day.Active;
                uiMonitorDayCommentary.Text = entry.Day.Commentary;
                uiDayValueUpDown.Value = (decimal)entry.Day.Value;
            }
        }

        private void MonitorStatisticsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Hide();
            e.Cancel = true;
        }
    }
}
